package cn.tedu.bonfire.admin.content.controller;

import cn.tedu.bonfire.admin.content.pojo.dto.CategoryAddNewDto;
import cn.tedu.bonfire.admin.content.pojo.dto.CategoryUpdateInfoDto;
import cn.tedu.bonfire.admin.content.pojo.vo.CategoryListItemVO;
import cn.tedu.bonfire.admin.content.pojo.vo.CategoryStandardVO;
import cn.tedu.bonfire.admin.content.pojo.vo.TypeListItemVO;
import cn.tedu.bonfire.admin.content.service.ICategoryService;
import cn.tedu.bonfire.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.bonfire.common.pojo.vo.PageData;
import cn.tedu.bonfire.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Api(tags = "1。分类模块")
@Validated
@RestController
@RequestMapping("/admin/type")
public class CategoryController {
    @Autowired
    private ICategoryService CategoryService;

    public CategoryController(){
        log.debug("创建控制器类对象：CategoryController");
    }

    @PostMapping("/insert")
    @ApiOperation("添加分类")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid CategoryAddNewDto categoryAddNewDto){
        log.debug("开始处理【添加类别】的请求，参数：{}", categoryAddNewDto);
        CategoryService.addNew(categoryAddNewDto);
        return JsonResult.ok();
    }

    @PostMapping("/delete/{id:[0-9]+}")
    @ApiOperation("根据id删除类别")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult addNew(@PathVariable @Range(min = 1,message = "请提交有效的类别ID值！")Long id){
        log.debug("开始处理【根据ID删除类别】的请求，参数：{}", id);
        CategoryService.delete(id);
        return JsonResult.ok();
    }
    @PostMapping("/update/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/content/category/simple')")
    @ApiOperation("修改类别详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id,
                                     @Valid CategoryUpdateInfoDto typeUpdateInfo) {
        log.debug("开始处理【修改类别详情】的请求，ID：{}，新数据：{}", id, typeUpdateInfo);
        CategoryService.updateInfoById(id, typeUpdateInfo);
        return JsonResult.ok();
    }


    @ApiOperation("根据ID查询类别详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    @GetMapping("/listParentCategory{id:[0-9]+}")
    public JsonResult getStandardById(@Range(min = 1, message = "查询类别详情失败，请提交有效的ID值！")
                                      @PathVariable Long id) {
        CategoryStandardVO queryResult = CategoryService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @ApiOperation("根据父级查询子级类别列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父级类别ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int")
    })
    @GetMapping("/listType/ByparentId")
    public JsonResult getStandardById(@Range(message = "查询子级类别列表失败，请提交有效的父级类别ID值！") Long parentId,
                                      @Range(min = 1, message = "查询子级类别列表失败，请提交有效的页码！") Integer page) {
        Integer pageNum = page == null ? 1 : page;
        PageData<CategoryListItemVO> pageData = CategoryService.listByParent(parentId, pageNum, 5);
        return JsonResult.ok(pageData);
    }


    @GetMapping("/listType")
    @ApiOperation("获取类型列表")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = true, dataType = "Integer")
    })
    public JsonResult listType(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal,
                               @Range(min = 1, message = "请提供有效的页码数") Integer pageNum,
                               @Range(min = 1, message = "请提供有效的页码记录数") Integer pageSize) {
        log.info("获取类型列表");
        //判断pageNum是否为空，如果为空则设置为1
        pageNum = pageNum == null ? 1 : pageNum;
        PageData<TypeListItemVO> pageData = CategoryService.listType(pageNum, pageSize);
        return JsonResult.ok(pageData);
    }
}
